package client

import (
	"context"
	"github.com/micro/go-micro/v2/client"
	"github.com/micro/go-micro/v2/client/grpc"

	"kop/framework/wrapper"
	"kop/modules/building/entity"
	"kop/pb"
	"kop/pb/service"
)

var BuildingService service.BuildingService

func init() {

	var c = grpc.NewClient(client.Wrap(wrapper.ClientLogWrapper))
	_ = c.Init()

	//s.Init(test.Flags()...)

	BuildingService = service.NewBuildingService("go.micro.srv.captain", c)
}

func GetBuildings(roleID int32) []*pb.Building {

	var resp, err = BuildingService.GetBuildings(context.Background(), &service.RoleRequest{RoleID: roleID})
	if err != nil {
		panic(err)
	}

	return resp.Buildings
}

func GetHall(roleID int32) *pb.Building {

	var building, err = BuildingService.GetBuilding(context.Background(), &service.DefaultRequest{RoleID: roleID, Value: entity.HALL})
	if err != nil {
		panic(err)
	}
	return building.Building
}

func GetBuilding(roleID, buildingID int32) *pb.Building {

	var building, err = BuildingService.GetBuilding(context.Background(), &service.DefaultRequest{RoleID: roleID, Value: buildingID})
	if err != nil {
		panic(err)
	}
	return building.Building
}

func Save(building *pb.Building) {

	var b, err = BuildingService.Save(context.Background(), building)
	if err != nil {
		panic(err)
	}
	*building = *b
}

func ResetCoin(ctx context.Context, shipID int32, captains []*pb.Captain) {

	_, _ = BuildingService.ResetCoin(ctx, &service.ResetCoinRequest{ShipID: shipID, Captains: captains})
}

//func ResetAllCoins(roleID int32) ([]*pb.Building, error) {
//
//	var resp, err = BuildingService.ResetAllCoins(context.Background(), &service.RoleRequest{RoleID: roleID})
//	if err != nil {
//		return nil, err
//	} else {
//		return resp.Buildings, nil
//	}
//}
